﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ESRI.ArcGIS.Client.Geometry;

namespace Skybot.Arcgis.Client.TileLayerExpand
{
    using System.Linq;
    /// <summary>
    /// 轉換資料
    /// </summary>
    public static class CoordTransformForShangHai
    {
        /// <summary>
        /// 轉換這個Map Point 到我國的火星坐標
        /// </summary>
        /// <param name="mapPoint">當前點</param>
        /// <param name="ZoomLevel">縮放等級</param>
        public static MapPoint WGS84CoordTranformChina(this MapPoint mapPoint)
        {
            return ChinaCoordTranform(mapPoint, 16, TransformType.WGS84CoordTranformChina);
        }

        /// <summary>
        /// 轉換我國的火星坐標 Map Point 到 WGS84
        /// </summary>
        /// <param name="mapPoint">當前點</param>
        /// <param name="ZoomLevel">縮放等級</param>
        public static MapPoint ChinaCoordTranformToWGS84(this MapPoint mapPoint)
        {
            return ChinaCoordTranform(mapPoint, 16, TransformType.ChinaCoordTranformToWGS84);
        }


        public static MapPoint ChinaCoordTranform(MapPoint mapPoint, int ZoomLevel, TransformType transformType)
        {
            MapPoint point = new MapPoint();

            try
            {


                //精確算法精度高,計算量大
                // SkybotCoord coord = ShangHaiTransFormData.ShaHaiCoordTransform.First(p => p.Longitude == double.Parse(mapPoint.X.ToString("f2")) && p.Lat == double.Parse(mapPoint.Y.ToString("f2")));

                #region 用間隔
                //2間隔
                //SkybotCoord coord = ShangHaiTransFormData.ShaHaiCoordTransformInterval2.First(p => 
                //   ( p.Longitude == double.Parse(mapPoint.X.ToString("f2"))+0.01 ||p.Longitude == double.Parse(mapPoint.X.ToString("f2"))-0.01)
                //    &&
                //   ( p.Lat == double.Parse(mapPoint.Y.ToString("f2")) + 0.01 || p.Lat == double.Parse(mapPoint.Y.ToString("f2")) - 0.01)                    
                //    );
                //3間隔
                //SkybotCoord coord = ShangHaiTransFormData.ShaHaiCoordTransformInterval6.First(p =>
                //   (
                //   p.Longitude == double.Parse(mapPoint.X.ToString("f2")) + 0.06 || p.Longitude == double.Parse(mapPoint.X.ToString("f2")) - 0.06
                //   ||
                //   p.Longitude == double.Parse(mapPoint.X.ToString("f2")) + 0.05 || p.Longitude == double.Parse(mapPoint.X.ToString("f2")) - 0.05
                //   ||
                //   p.Longitude == double.Parse(mapPoint.X.ToString("f2")) + 0.04 || p.Longitude == double.Parse(mapPoint.X.ToString("f2")) - 0.04
                //   ||
                //   p.Longitude == double.Parse(mapPoint.X.ToString("f2")) + 0.03 || p.Longitude == double.Parse(mapPoint.X.ToString("f2")) - 0.03
                //   ||
                //   p.Longitude == double.Parse(mapPoint.X.ToString("f2")) + 0.02 || p.Longitude == double.Parse(mapPoint.X.ToString("f2")) - 0.02
                //   ||
                //   p.Longitude == double.Parse(mapPoint.X.ToString("f2")) + 0.01 || p.Longitude == double.Parse(mapPoint.X.ToString("f2")) - 0.01
                //   ||
                //    p.Longitude == double.Parse(mapPoint.X.ToString("f2"))
                //   )
                //    &&
                //   (
                //   p.Lat == double.Parse(mapPoint.Y.ToString("f2")) + 0.06 || p.Lat == double.Parse(mapPoint.Y.ToString("f2")) - 0.06
                //   ||
                //   p.Lat == double.Parse(mapPoint.Y.ToString("f2")) + 0.05 || p.Lat == double.Parse(mapPoint.Y.ToString("f2")) - 0.05
                //   ||
                //   p.Lat == double.Parse(mapPoint.Y.ToString("f2")) + 0.04 || p.Lat == double.Parse(mapPoint.Y.ToString("f2")) - 0.04
                //   ||
                //   p.Lat == double.Parse(mapPoint.Y.ToString("f2")) + 0.03 || p.Lat == double.Parse(mapPoint.Y.ToString("f2")) - 0.03
                //   ||
                //   p.Lat == double.Parse(mapPoint.Y.ToString("f2")) + 0.02 || p.Lat == double.Parse(mapPoint.Y.ToString("f2")) - 0.02
                //   ||
                //   p.Lat == double.Parse(mapPoint.Y.ToString("f2")) + 0.01 || p.Lat == double.Parse(mapPoint.Y.ToString("f2")) - 0.01
                //   ||
                //    p.Lat == double.Parse(mapPoint.Y.ToString("f2"))
                //   )
                //    );
                #endregion
                //0.1精度計算
                //SkybotCoord coord = ShangHaiTransFormData.ShaHaiCoordTransformInterval10.First(p => p.Longitude == double.Parse(mapPoint.X.ToString("f1")) && p.Lat == double.Parse(mapPoint.Y.ToString("f1")));
                //0.001精度计算
                SkybotCoord coord = ShangHaiTransFormData.DictionaryshaHaiCoordTransform[mapPoint.X.ToString("f2")+","+mapPoint.Y.ToString("f2")];


                int PxX = 0;
                int PxY = 0;
                // 定義臨時變量
                double Lat = 0;
                double Longitude = 0;

                TileSystem.LatLongToPixelXY(mapPoint.Y, mapPoint.X, ZoomLevel, out PxX, out PxY);

                #region 處理各不同級別的偏移量 目前資料為18級的

                int offsetX = 0;
                int offsetY = 0;

                //如果小於18級
                if (ZoomLevel < 18)
                {
                    //数据介绍  依次为  经度  纬度  此位置偏移像素X,Y（google地图的第18级偏移量，每向上一级除2）
                    //http://blog.csdn.net/dongmeng110/archive/2010/03/26/5418678.aspx
                    //得到不同級別的偏移量
                    offsetX = int.Parse(Math.Round(coord.X / ((18 - ZoomLevel) * 2.0)).ToString());
                    offsetY = int.Parse(Math.Round(coord.Y / ((18 - ZoomLevel) * 2.0)).ToString());

                    if (transformType == TransformType.WGS84CoordTranformChina)
                    {
                        //得到偏移後的 像素值
                        PxY += offsetY;
                        PxX += offsetX;
                    }

                    if (transformType == TransformType.ChinaCoordTranformToWGS84)
                    {
                        //得到偏移後的 像素值
                        PxY -= offsetY;
                        PxX -= offsetX;
                    }
                }
                else
                {
                    if (transformType == TransformType.WGS84CoordTranformChina)
                    {
                        //得到偏移後的 像素值
                        PxY += coord.X;
                        PxX += coord.Y;
                    }
                    if (transformType == TransformType.ChinaCoordTranformToWGS84)
                    {
                        //得到偏移後的 像素值
                        PxY -= coord.X;
                        PxX -= coord.Y;
                    }

                }


                #endregion

                TileSystem.PixelXYToLatLong(PxX, PxY, ZoomLevel, out Lat, out Longitude);

                //得到新的坐標
                point.X = Longitude;
                point.Y = Lat;
            }
            catch (InvalidOperationException)
            {
                throw new InvalidOperationException("沒有找到對應的坐標偏移量,请确认系统是否已经存在当前地区的偏移量数据");
            }


            return point;
        }
    }

    /// <summary>
    /// 轉換類型
    /// </summary>
    public enum TransformType
    {
        /// <summary>
        /// 中國地圖坐標轉換到 WGS84
        /// </summary>
        ChinaCoordTranformToWGS84,
        //WGS84轉換到中國地圖
        WGS84CoordTranformChina
    }
}



 
